package vtt.android.cartoon_video.database;

import java.util.ArrayList;
import java.util.List;

import vtt.android.cartoon_video.BaseApplication;
import vtt.android.cartoon_video.entities.Category;
import vtt.android.library.utils.VttLog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CategoryDAO extends BaseDAO {

    private static CategoryDAO instance;
    VttLog log = new VttLog("CategoryDAO", BaseApplication.isDebuggable);
    // field database
    public static String TABLE_NAME = "category";
    public static final String COL_TYPE = "type";
    public static final String COL_NAME = "name";
    public static final String COL_ACTIVE = "active";

    public CategoryDAO(Context context) {
        super(context);
    }
    
    public static CategoryDAO getInstance(Context context){
        if(instance == null){
            instance = new CategoryDAO(context);
        }
        return instance;
    }

	/**
     * create table chuyen muc
     * @param db
     */
	void createTable(SQLiteDatabase db) {
        String sql = " CREATE TABLE " + TABLE_NAME + " ( " 
                + " type TEXT, " 
                + " title TEXT, "
                + " search_keyword TEXT, "
                + " sort_order INT, "
                + " active INT DEFAULT ( 1 ) "
                + ");";
        db.execSQL(sql);
        
        String query = "INSERT INTO [" + TABLE_NAME +"] ([type], [title], [search_keyword], [sort_order], [active]) VALUES ";
        db.execSQL(query + "('cartoon', 'Tom and Jerry', 'Tom and Jerry', 0, 1);");
        db.execSQL(query + "('cartoon', 'Oscar', 'oscar oasis', 1, 1);");
        db.execSQL(query + "('cartoon', 'Oggy', 'oggy', 2, 1);");
    }

    public List<Category> getCategory(boolean isActive) {
        List<Category> itemList = new ArrayList<Category>();
        // Select All Query
        String selectQuery = " SELECT  type, title, search_keyword, active " 
                + " FROM " + TABLE_NAME 
                + " WHERE active = " + (isActive == true ? "1" : "0")
                + " ORDER BY sort_order ASC" ;
        log.d("VTT>>>getCategory selectQuery " + selectQuery);
        Cursor cursor = mDB.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Category rssSource = new Category();
                rssSource.setType(cursor.getString(0));
                rssSource.setTitle(cursor.getString(1));
                rssSource.setSearchKey(cursor.getString(2));
                rssSource.setActive(cursor.getInt(3) == 1 ? true : false);
                // add item into list
                itemList.add(rssSource);
            } while (cursor.moveToNext());
        }

        return itemList;
    }
}
